package net {

	import common.HTTPService;
	import common.loader.BaseLoader;
	
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
	
	import globals.Setting;
	
	import net.message.BaseMessage;
	import net.message.GCUserMessage;

	public class ServiceManager {
		
		public var messageMap:Dictionary;

		public function ServiceManager() {
			messageMap = new Dictionary();
		}
		
		public function init():void {
			messageMap[MessageType.GC_USER_INFO] = new GCUserMessage();
		}
		
		public function send(message:BaseMessage):void {
			var loader:HTTPService = new HTTPService(Setting.SERVER_HOST);
			loader.handler = serviceHandler;
			loader.send(message);
		}
		
		private function serviceHandler(byteArray:IDataInput):void {
			trace("server response complete");
			var length:uint = byteArray.readInt();
			var cmd:uint = byteArray.readShort();
			var message:BaseMessage = messageMap[cmd];
			if (message != null) {
				var body:ByteArray = new ByteArray();
				byteArray.readBytes(body);
				body.position = 0;
				message.readMessage(body);
				message.handlerMessage();
			}
		}
	}
}